package com.jshx.sz.gdjt.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Message;

import com.jshx.sz.gdjt.constant.ApplicationGlobal;

public class AccessDBImp implements IAccessDB {

	@Override
	public String proDB(String actionName, String params) throws Exception {
		String detailUrl=ApplicationGlobal.serviceUrl
		+"strTemp=%7B'classname':'GdjtReq','actionName':'"+actionName+"'"+params+"%7D";
		//detailUrl=ApplicationGlobal.serviceUrl;
		StringBuffer sb = new StringBuffer();
		
		HttpClient client = new DefaultHttpClient();
		HttpParams httpParams = client.getParams();
		HttpConnectionParams.setConnectionTimeout(httpParams, 20000);
		HttpConnectionParams.setSoTimeout(httpParams, 20000);
		HttpPost post = new HttpPost(detailUrl);
		HttpResponse response = client.execute(post);
		Header header = response.getFirstHeader("exception");
		if (header != null) {
			throw new Exception();
		} else {
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(entity.getContent(), "UTF-8"),
						8192);

				String line = null;
				while ((line = reader.readLine()) != null) {
					sb.append(line + "\n");
				}
				reader.close();
			}
			
			return  sb.toString(); 
		}
	}

	@Override
	public HashMap splitDataHashMap(String jsString  ) throws JSONException {
		HashMap<String, String> returnMap=new HashMap<String, String>();
		if (jsString.length() > 0) 
		{
			if (jsString != null && !jsString.trim().equals("")) 
			{
				JSONObject jo = new JSONObject(jsString);
				Iterator keys =jo.keys();
				while (keys.hasNext())
				{
					Object key = keys.next();
					Object value = jo.get(key+"");
					returnMap.put(key+"", value+"");
				}
				
				keys=null;
				String contentStr=jo.getString("content");
				if(contentStr!=null&&!contentStr.equals(""))
				{
					JSONObject content = new JSONObject(contentStr);
					keys =content.keys();
					while (keys.hasNext())
					{
						Object key = keys.next();
						Object value = content.get(key+"");
						returnMap.put(key+"", value+"");
					}
				}
			}
		}
		return returnMap;
	}

	@Override
	public ArrayList splitDataList(String jsString ) throws Exception {
		ArrayList  returnList=new ArrayList ();
		if (jsString.length() > 0) 
		{
			if (jsString != null && !jsString.trim().equals("")) 
			{
				JSONObject jo = new JSONObject(jsString);
				 
				String contentStr=jo.getString("content");
				if(contentStr!=null&&!contentStr.equals(""))
				{
					JSONArray content = new JSONArray(contentStr);
					for(int i=0;i<content.length();i++)
					{	
						HashMap<String, String> map=new HashMap<String, String>();
						JSONObject jobj=content.getJSONObject(i);
						Iterator keys = jobj.keys();
						while (keys.hasNext())
						{
							Object key = keys.next();
							Object value = jobj .get(key+"");
							map.put(key+"", value+"");
						}
						returnList.add(map);
					}
				}
			}
		}
		return   returnList;
	}
	
	/**
	 * 数据拷贝
	 * @param res 源
	 * @param obj 目标
	 */
	public void dataCopyHashMapToHashMap(HashMap res,HashMap obj)
	{
		if(res!=null&&res.size()>0)
		{
			Iterator iter=res.entrySet().iterator();
			while(  iter.hasNext() )
			{
				 Entry entry = (Entry)iter.next();
				 Object key=entry.getKey();   
				 Object value=entry.getValue();  
				 obj.put(key, value);
			}
		}
	}

	/**
	 * 数据拷贝
	 * @param res 源
	 * @param obj 目标
	 */
	public void dataCopyArrayListToArrayList(ArrayList res,ArrayList obj)
	{
		if(res!=null&&res.size()>0)
		{
			for(int i=0;i<res.size();i++)
			{
				obj.add(res.get(i));
			}
		}
	}


}
